var  api = require("../../../utils/api.js"),
    WXBizDataCrypt  = require("../../../../utils/RdWXBizDataCrypt.js"),

n = getApp();

Page({
    data: {
        name: "",
        code: '',
        laiyuan: "",
         tel: ""
        // tabBar: []
    },
    getPhoneNumber(e) {
      console.log(e.detail.code)  // 动态令牌
      console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
      console.log(e.detail.errno)  // 错误码（失败时返回）
      if (!e.detail) {
        return;
      }
      wx.login({
        success: function(res) {
        if (res.code) {
         // 成功获取 code，后续发送到服务器调换 session_key 和 openid
          console.log('Login success with code: ', res.code);
          uni.request({
            url: `https://api.weixin.qq.com/sns/jscode2session?appid=XXX&secret=XXX&js_code=${res.code}&grant_type=authorization_code`,
            method: 'GET',
            dataType: 'json',
            sslVerify: false,
            success: res => {
              console.log('session-res', res);
              if (!res) {
                return;
              }
              this.openid = res.data.openid;
              let phone = e.detail.encryptedData;
              let iv = e.detail.iv;
              // const RdWXBizDataCrypt = require('../../utils/RdWXBizDataCrypt.js');
              let appId = 'XXX';
              let session_key = res.data.session_key;
              const pc = new WXBizDataCrypt (appId, session_key);
              const data = pc.decryptData(phone, iv);
              if (data) {
                this.setData({tel:data.phoneNumber })
              }
              console.log('data', data);
            },
            fail: err => {
              console.error(err);
            }
          });

        } else {
          console.log('Login failed. Error: ', res.errMsg);
        }
       }
        });
      
    },
    getinput(event){
      console.log(event)
      //console.log(detail.value)
    },
    toMenu: function(t) {
        var e = this, n = t.currentTarget.dataset.url;
        switch (t.currentTarget.dataset.id) {
          case "电话":
            e.makeToPhone();
            break;

          case "导航":
            e.showinfo();
            break;

          default:
            wx.reLaunch({
                url: n
            });
        }
    },




    
    showinfo: function(t) {
        wx.openLocation({
            latitude: e.latitude,
            longitude: e.longitude,
            scale: 18,
            name: e.QYMC,
            address: e.ADDR
        });
    },
    makeToPhone: function() {
        wx.makePhoneCall({
            phoneNumber: e.TEL
            // this.setData({phoneNumber:'xxxxxx' })
        });
    },
    formReset: function() {
        this.setData({
            lxr: "",
            nr: "",
            tel: ""
        });
    },
    formSubmit: function(e) {
        var n = this, a = e.detail.value.name, o = e.detail.value.laiyuan, i = e.detail.value.tel;
        if ("" == a || "" == o || "" == i) wx.showModal({
            title: "提示",
            content: "请完整填写！",
            confirmText: "确定",
            showCancel: !1
        }); else {
            wx.showLoading({
                title: "数据保存中"
            });
            var r = e.detail.value;
            api.add_message(r).then(function(t) {
                wx.hideLoading(), wx.showToast({
                    title: "提交成功！",
                    duration: 2e3,
                    success: function(t) {
                        n.formReset();
                    }
                });
            });
        }
    },
    onLoad: function() {
        for (var t = this, a = n.globalData.tabBar, o = 0; o < e.MENUS; o++) a.list[o].active = !1;
        t.setData({
            tabBar: a
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});